﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace my_serialisation
{
    class Program
    {
        static void Main(string[] args)
        {
            MyClass c = new MyClass("Toto");
            serializer(c, "File.bin");
            c = (MyClass)deserializer("File.bin");

            Console.WriteLine(c.s);
            Console.ReadLine();
        }

        static void serializer(object o, string file)
        {
            IFormatter formatter = new BinaryFormatter();

            Stream stream = new FileStream(file, FileMode.Create);

            formatter.Serialize(stream, o);
            stream.Close();
        }

        static object deserializer(string file)
        {
            Stream stream = new FileStream(file, FileMode.Open);

            IFormatter formatter = new BinaryFormatter();

            object myobject = formatter.Deserialize(stream);

            stream.Close();

            return myobject;
        }
    }
}
